package com.touzhijia.authority.controller;

import com.github.pagehelper.PageInfo;
import com.touzhijia.authority.domain.dto.request.*;
import com.touzhijia.authority.domain.dto.response.UserModuleAddRes;
import com.touzhijia.authority.domain.dto.response.UserRoleAddRes;
import com.touzhijia.authority.domain.entity.User;
import com.touzhijia.authority.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者： lzw<br/>
 * 创建时间：2018-03-14 14:28 <br/>
 */
@Api(description = "用户管理")
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取用户信息")
    @GetMapping("/user/{username}")
    public User getUser(@PathVariable("username") String username) {
        return userService.getUser(username);
    }

    @ApiOperation("查询用户信息")
    @GetMapping("/user")
    public PageInfo<User> findUser(@Validated UserQueryReq queryRes) {
        return userService.findUser(queryRes);
    }

    @ApiOperation("新增用户信息")
    @PostMapping("/user")
    public User addUser(@Validated @RequestBody UserAddReq userAddReq) {
        return userService.addUser(userAddReq);
    }

    @ApiOperation("删除用户信息")
    @DeleteMapping("/user/{username}")
    public User delUser(@PathVariable("username") String username) {
        return userService.delUser(username);
    }

    @ApiOperation("修改用户信息")
    @PutMapping("/user/{username}")
    public User updateUser(@PathVariable("username") String username, @Validated @RequestBody UserUpdateReq userUpdateReq) {
        return userService.updateUser(username, userUpdateReq);
    }

    @ApiOperation("新增用户角色")
    @PostMapping("/user/role")
    public UserRoleAddRes addUserRole(@Validated @RequestBody UserRoleAddReq addReq) {
        return userService.addUserRole(addReq);
    }

    @ApiOperation("删除用户角色")
    @DeleteMapping("/user/role/{username}/{roleName}")
    public UserRoleAddRes removeUserRole(@PathVariable("username") String username, @PathVariable("roleName") String roleName) {
        return userService.removeUserRole(username, roleName);
    }

    @ApiOperation("新增用户模块")
    @PostMapping("/user/module")
    public UserModuleAddRes addUserModule(@Validated @RequestBody UserModuleAddReq addReq) {
        return userService.addUserModule(addReq);
    }

    @ApiOperation("删除用户模块")
    @DeleteMapping("/user/module/{username}/{module}")
    public UserModuleAddRes delUserModule(@PathVariable("username") String username, @PathVariable("module") String module) {
        return userService.delUserModule(username, module);
    }

    @ApiOperation("查询用户所有模块")
    @GetMapping("/user/module/{username}")
    public List<String> findModuleByUser(@PathVariable("username") String username) {
        return userService.findModuleByUser(username);
    }
}
